import distributedKVStore from '@ohos.data.distributedKVStore';
import HashMap from '@ohos.util.HashMap';
export class DataKvUtil {
    getStoreId() {
        return this.mStoreId;
    }
    constructor() {
        this.mKvStoreHashMap = new HashMap();
        this.mStoreId = "abner_store_kv";
        this.mIsLoadKVStore = false;
    }
    static getInstance() {
        if (!DataKvUtil.mDataKvUtil) {
            DataKvUtil.mDataKvUtil = new DataKvUtil();
        }
        return DataKvUtil.mDataKvUtil;
    }
    init(s7, t7, u7) {
        const v7 = {
            context: s7,
            bundleName: t7
        };
        try {
            this.mBundleName = t7;
            this.mKvManager = distributedKVStore.createKVManager(v7);
            this.createKVStore(this.mStoreId, u7);
        }
        catch (w7) {
            let x7 = w7;
            console.error(`Failed to create KVManager. Code:${x7.code},message:${x7.message}`);
        }
        return this;
    }
    initPromise(o7, p7, q7) {
        const r7 = {
            context: o7,
            bundleName: p7
        };
        this.mBundleName = p7;
        this.mKvManager = distributedKVStore.createKVManager(r7);
        return this.mKvManager?.getKVStore(this.mStoreId, this.getOptions(q7));
    }
    initSuccess(n7) {
        this.mKVStoreCallBack = n7;
        return this;
    }
    getOptions(l7) {
        let m7 = {
            createIfMissing: true,
            encrypt: true,
            backup: true,
            autoSync: true,
            kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
            securityLevel: distributedKVStore.SecurityLevel.S3,
        };
        if (l7 != undefined) {
            if (l7.createIfMissing != undefined) {
                m7.createIfMissing = l7.createIfMissing;
            }
            if (l7.encrypt != undefined) {
                m7.encrypt = l7.encrypt;
            }
            if (l7.backup != undefined) {
                m7.backup = l7.backup;
            }
            if (l7.autoSync != undefined) {
                m7.autoSync = l7.autoSync;
            }
            if (l7.kvStoreType != undefined) {
                m7.kvStoreType = l7.kvStoreType;
            }
            if (l7.securityLevel != undefined) {
                m7.securityLevel = l7.securityLevel;
            }
            if (l7.schema != undefined) {
                m7.schema = l7.schema;
            }
        }
        return m7;
    }
    createKVStore(e7, f7) {
        try {
            if (this.isKvManagerNoEmpty()) {
                this.mKvManager?.getKVStore(e7, this.getOptions(f7), (j7, k7) => {
                    if (j7) {
                        console.error(`Failed to get KVStore.code is ${j7.code},message is ${j7.message}`);
                        return;
                    }
                    this.mKvStoreHashMap.set(e7, k7);
                    if (this.mKVStoreCallBack != undefined) {
                        this.mKVStoreCallBack();
                    }
                });
            }
        }
        catch (g7) {
            let h7 = g7;
            console.error(`An unexpected error occurred.code is ${h7.code},message is ${h7.message}`);
        }
    }
    isKvManagerNoEmpty() {
        if (this.mKvManager == undefined) {
            console.error("ERROR:是不是忘记了初始化方法init");
            return false;
        }
        return true;
    }
    isKVStoreEmpty() {
        if (!this.mIsLoadKVStore) {
            this.setKVStore();
        }
        this.mIsLoadKVStore = false;
    }
    setKVStore(d7) {
        if (d7 == undefined) {
            this.mKvStore = this.mKvStoreHashMap.get(this.mStoreId);
        }
        else {
            this.mIsLoadKVStore = true;
            this.mKvStore = this.mKvStoreHashMap.get(d7);
        }
        return this;
    }
    getKVStore() {
        return this.mKvStore;
    }
    closeKVStore(z6, a7) {
        if (this.isKvManagerNoEmpty()) {
            this.mKvManager?.closeKVStore(this.mBundleName, z6, (c7) => {
                if (c7 != undefined) {
                    if (a7 != undefined) {
                        a7(false, c7);
                    }
                    return;
                }
                if (a7 != undefined) {
                    a7(true);
                }
            });
        }
    }
    deleteKVStore(v6, w6) {
        if (this.isKvManagerNoEmpty()) {
            this.mKvManager?.deleteKVStore(this.mBundleName, v6, (y6) => {
                if (y6 != undefined) {
                    if (w6 != undefined) {
                        w6(false, y6);
                    }
                    return;
                }
                if (w6 != undefined) {
                    w6(true);
                }
            });
        }
    }
    getAllKVStoreId(o6, p6) {
        try {
            if (this.isKvManagerNoEmpty()) {
                this.mKvManager?.getAllKVStoreId(this.mBundleName, (t6, u6) => {
                    if (t6 != undefined) {
                        if (p6 != undefined) {
                            p6(t6);
                        }
                        return;
                    }
                    o6(u6);
                });
            }
        }
        catch (q6) {
            let r6 = q6;
            if (p6 != undefined) {
                p6(r6);
            }
        }
    }
    onDistributedDataServiceDie(l6) {
        try {
            if (this.isKvManagerNoEmpty()) {
                this.mKvManager?.on('distributedDataServiceDie', l6);
            }
        }
        catch (m6) {
            let n6 = m6;
            console.error(`An unexpected error occurred.code is ${n6.code},message is ${n6.message}`);
        }
    }
    put(e6, f6, g6) {
        try {
            if (!this.mIsLoadKVStore) {
                this.setKVStore();
            }
            this.mKvStore?.put(e6, f6, (k6) => {
                if (k6 != undefined) {
                    if (g6 != undefined) {
                        g6(false, k6);
                    }
                    this.mIsLoadKVStore = false;
                    return;
                }
                if (g6 != undefined) {
                    g6(true);
                }
                this.mIsLoadKVStore = false;
            });
        }
        catch (h6) {
            let i6 = h6;
            if (g6 != undefined) {
                g6(false, i6);
            }
            this.mIsLoadKVStore = false;
        }
    }
    putPromise(c6, d6) {
        this.isKVStoreEmpty();
        return this.mKvStore.put(c6, d6);
    }
    putBatch(w5, x5) {
        try {
            if (!this.mIsLoadKVStore) {
                this.setKVStore();
            }
            this.mKvStore?.putBatch(w5, async (b6) => {
                if (b6 != undefined) {
                    if (x5 != undefined) {
                        x5(false, b6);
                    }
                    this.mIsLoadKVStore = false;
                    return;
                }
                if (x5 != undefined) {
                    x5(true);
                }
                this.mIsLoadKVStore = false;
            });
        }
        catch (y5) {
            let z5 = y5;
            if (x5 != undefined) {
                x5(false, z5);
            }
            this.mIsLoadKVStore = false;
        }
    }
    putBatchPromise(v5) {
        this.isKVStoreEmpty();
        return this.mKvStore.putBatch(v5);
    }
    delete(r5, s5) {
        this.isKVStoreEmpty();
        this.mKvStore?.delete(r5, (u5) => {
            if (u5 != undefined) {
                if (s5 != undefined) {
                    s5(false, u5);
                }
                return;
            }
            if (s5 != undefined) {
                s5(true);
            }
        });
    }
    deletePromise(q5) {
        this.isKVStoreEmpty();
        return this.mKvStore.delete(q5);
    }
    deleteBatch(m5, n5) {
        this.isKVStoreEmpty();
        this.mKvStore?.deleteBatch(m5, async (p5) => {
            if (p5 != undefined) {
                if (n5 != undefined) {
                    n5(false, p5);
                }
                return;
            }
            if (n5 != undefined) {
                n5(true);
            }
        });
    }
    deleteBatchPromise(l5) {
        this.isKVStoreEmpty();
        return this.mKvStore.deleteBatch(l5);
    }
    removeDeviceData(h5, i5) {
        this.isKVStoreEmpty();
        this.mKvStore?.removeDeviceData(h5, async (k5) => {
            if (k5 == undefined) {
                if (i5 != undefined) {
                    i5(true);
                }
            }
            else {
                if (i5 != undefined) {
                    i5(false, k5);
                }
            }
        });
    }
    removeDeviceDataPromise(g5) {
        this.isKVStoreEmpty();
        return this.mKvStore.removeDeviceData(g5);
    }
    get(a5, b5, c5) {
        this.isKVStoreEmpty();
        this.mKvStore?.get(a5, (e5, f5) => {
            if (e5 != undefined) {
                if (c5 != undefined) {
                    c5(e5);
                }
                return;
            }
            b5(f5);
        });
    }
    getPromise(z4) {
        this.isKVStoreEmpty();
        return this.mKvStore.get(z4);
    }
    getEntries(t4, u4, v4) {
        this.isKVStoreEmpty();
        this.mKvStore?.getEntries(t4, (x4, y4) => {
            if (x4 != undefined) {
                if (v4 != undefined) {
                    v4(x4);
                }
                return;
            }
            u4(y4);
        });
    }
    getEntriesPromise(s4) {
        this.isKVStoreEmpty();
        return this.mKvStore.getEntries(s4);
    }
    getQueryEntries(m4, n4, o4) {
        this.isKVStoreEmpty();
        this.mKvStore?.getEntries(m4, (q4, r4) => {
            if (q4 != undefined) {
                if (o4 != undefined) {
                    o4(q4);
                }
                return;
            }
            n4(r4);
        });
    }
    getQueryEntriesPromise(l4) {
        this.isKVStoreEmpty();
        return this.mKvStore.getEntries(l4);
    }
    getResultSet(f4, g4, h4) {
        this.isKVStoreEmpty();
        this.mKvStore?.getResultSet(f4, async (j4, k4) => {
            if (j4 != undefined) {
                if (h4 != undefined) {
                    h4(j4);
                }
                return;
            }
            g4(k4);
        });
    }
    getResultSetPromise(e4) {
        this.isKVStoreEmpty();
        return this.mKvStore.getResultSet(e4);
    }
    getQueryResultSet(y3, z3, a4) {
        this.isKVStoreEmpty();
        this.mKvStore?.getResultSet(y3, async (c4, d4) => {
            if (c4 != undefined) {
                if (a4 != undefined) {
                    a4(c4);
                }
                return;
            }
            z3(d4);
        });
    }
    getQueryResultSetPromise(x3) {
        this.isKVStoreEmpty();
        return this.mKvStore.getResultSet(x3);
    }
    closeResultSet(s3, t3, u3) {
        this.isKVStoreEmpty();
        this.mKvStore?.closeResultSet(s3, (w3) => {
            if (w3 != undefined) {
                if (u3 != undefined) {
                    u3(w3);
                }
                return;
            }
            if (t3 != undefined) {
                t3();
            }
        });
    }
    closeResultSetPromise(r3) {
        this.isKVStoreEmpty();
        return this.mKvStore.closeResultSet(r3);
    }
    getResultSize(l3, m3, n3) {
        this.isKVStoreEmpty();
        this.mKvStore?.getResultSize(l3, async (p3, q3) => {
            if (p3 != undefined) {
                if (n3 != undefined) {
                    n3(p3);
                }
                return;
            }
            m3(q3);
        });
    }
    getResultSizePromise(k3) {
        this.isKVStoreEmpty();
        return this.mKvStore.getResultSize(k3);
    }
    backup(d3, e3, f3) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.backup(d3, (j3) => {
                if (j3) {
                    if (f3 != undefined) {
                        f3(j3);
                    }
                }
                else {
                    if (e3 != undefined) {
                        e3();
                    }
                }
            });
        }
        catch (g3) {
            let h3 = g3;
            if (f3 != undefined) {
                f3(h3);
            }
        }
    }
    backupPromise(c3) {
        this.isKVStoreEmpty();
        return this.mKvStore.backup(c3);
    }
    restore(v2, w2, x2) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.restore(v2, (b3) => {
                if (b3) {
                    if (x2 != undefined) {
                        x2(b3);
                    }
                }
                else {
                    if (w2 != undefined) {
                        w2();
                    }
                }
            });
        }
        catch (y2) {
            let z2 = y2;
            if (x2 != undefined) {
                x2(z2);
            }
        }
    }
    restorePromise(u2) {
        this.isKVStoreEmpty();
        return this.mKvStore.restore(u2);
    }
    deleteBackup(m2, n2, o2) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.deleteBackup(m2, (s2, t2) => {
                if (s2) {
                    if (o2 != undefined) {
                        o2(s2);
                    }
                }
                else {
                    if (n2 != undefined) {
                        n2(t2);
                    }
                }
            });
        }
        catch (p2) {
            let q2 = p2;
            if (o2 != undefined) {
                o2(q2);
            }
        }
    }
    deleteBackupPromise(l2) {
        this.isKVStoreEmpty();
        return this.mKvStore.deleteBackup(l2);
    }
    startTransaction(h2, i2) {
        this.isKVStoreEmpty();
        this.mKvStore?.startTransaction(async (k2) => {
            if (k2 != undefined) {
                if (i2 != undefined) {
                    i2(k2);
                }
                return;
            }
            if (h2 != undefined) {
                h2();
            }
        });
    }
    startTransactionPromise() {
        this.isKVStoreEmpty();
        return this.mKvStore.startTransaction();
    }
    commit(b2, c2) {
        try {
            this.isKVStoreEmpty();
            this.mKvStore?.commit((g2) => {
                if (g2 == undefined) {
                    if (b2 != undefined) {
                        b2();
                    }
                }
                else {
                    if (c2 != undefined) {
                        c2(g2);
                    }
                }
            });
        }
        catch (d2) {
            let e2 = d2;
            if (c2 != undefined) {
                c2(e2);
            }
        }
    }
    commitPromise() {
        this.isKVStoreEmpty();
        return this.mKvStore.commit();
    }
    rollback(v1, w1) {
        try {
            this.isKVStoreEmpty();
            this.mKvStore?.rollback((a2) => {
                if (a2 == undefined) {
                    if (v1 != undefined) {
                        v1();
                    }
                }
                else {
                    if (w1 != undefined) {
                        w1(a2);
                    }
                }
            });
        }
        catch (x1) {
            let y1 = x1;
            if (w1 != undefined) {
                w1(y1);
            }
        }
    }
    rollbackPromise() {
        this.isKVStoreEmpty();
        return this.mKvStore.rollback();
    }
    enableSync(p1, q1) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.enableSync(true, (u1) => {
                if (u1 == undefined) {
                    if (p1 != undefined) {
                        p1();
                    }
                }
                else {
                    if (q1 != undefined) {
                        q1(u1);
                    }
                }
            });
        }
        catch (r1) {
            let s1 = r1;
            if (q1 != undefined) {
                q1(s1);
            }
        }
    }
    enableSyncPromise() {
        this.isKVStoreEmpty();
        return this.mKvStore.enableSync(true);
    }
    setSyncRange(h1, i1, j1, k1) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.setSyncRange(h1, i1, (o1) => {
                if (o1 != undefined) {
                    if (k1 != undefined) {
                        k1(o1);
                    }
                    return;
                }
                if (j1 != undefined) {
                    j1();
                }
            });
        }
        catch (l1) {
            let m1 = l1;
            if (k1 != undefined) {
                k1(m1);
            }
        }
    }
    setSyncRangePromise(f1, g1) {
        this.isKVStoreEmpty();
        return this.mKvStore.setSyncRange(f1, g1);
    }
    setSyncParam(y, z, a1) {
        try {
            this.mKvStore?.setSyncParam(y, (e1) => {
                if (e1 != undefined) {
                    if (a1 != undefined) {
                        a1(e1);
                    }
                    return;
                }
                if (z != undefined) {
                    z();
                }
            });
        }
        catch (b1) {
            let c1 = b1;
            if (a1 != undefined) {
                a1(c1);
            }
        }
    }
    setSyncParamPromise(x) {
        this.isKVStoreEmpty();
        return this.mKvStore.setSyncParam(x);
    }
    sync(t, u, v, w) {
        this.isKVStoreEmpty();
        this.mKvStore.sync(t, u, v, w);
    }
    onDataChange(r, s = distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_LOCAL) {
        this.isKVStoreEmpty();
        this.mKvStore?.on('dataChange', s, r);
    }
    offDataChange(q) {
        this.isKVStoreEmpty();
        this.mKvStore?.off('dataChange', q);
    }
    onSyncComplete(p) {
        this.isKVStoreEmpty();
        this.mKvStore?.on('syncComplete', p);
    }
    offSyncComplete(o) {
        this.isKVStoreEmpty();
        this.mKvStore?.off('syncComplete', o);
    }
    getSecurityLevel(h, i) {
        this.isKVStoreEmpty();
        try {
            this.mKvStore?.getSecurityLevel((m, n) => {
                if (m != undefined) {
                    if (i != undefined) {
                        i(m);
                    }
                    return;
                }
                h(n);
            });
        }
        catch (j) {
            let k = j;
            if (i != undefined) {
                i(k);
            }
        }
    }
    getSecurityLevelPromise() {
        this.isKVStoreEmpty();
        return this.mKvStore.getSecurityLevel();
    }
    deviceGet(b, c, d) {
        this.mKvStore?.get(b, (f, g) => {
            if (f != undefined) {
                if (d != undefined) {
                    d(f);
                }
                return;
            }
            c(g);
        });
    }
    deviceGetPromise(a) {
        return this.mKvStore.get(a);
    }
}
